package com.minggo.charmword.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

import com.minggo.charmword.service.CharmWordApplication;

/**
 * 全局应用程序类：用于保存和调用全局应用配置及访问网络数据
 * 
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class AppContext {

	public static final int NETTYPE_WIFI = 0x01;
	public static final int NETTYPE_CMWAP = 0x02;
	public static final int NETTYPE_CMNET = 0x03;
	private static AppContext appContext;

	public static String user_filepath = CharmWordApplication.SDPATH + "user/"; // 保存到SD卡的目录

	public static final String PREFERENCE_BOOKV4 = "com.jiubang.bookv4";
	public static final String APPEXCEPTION = "appexception";
	public static final String NATIVE_BOOK_PATH = "native_book_path";
	public static final String NATIVE_BOOK_ID = "native_book_id";
	public static final String DOWNLOAD_PATH = "download";

	public AppContext() {
	}

	public static AppContext getInstance() {
		if (null == appContext) {
			appContext = new AppContext();
		}
		return appContext;
	}

	/**
	 * 检测网络是否可用
	 * 
	 * @return
	 */
	public boolean isNetworkConnected() {
		ConnectivityManager cm = (ConnectivityManager) CharmWordApplication.getInstance().getSystemService(
				Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni != null && ni.isConnectedOrConnecting()) {
			if (ni.getState() == NetworkInfo.State.CONNECTED) {
				return true;
			}
		}
		return false;
	}

	public static boolean isNetworkConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (ni != null && ni.isConnectedOrConnecting()) {
			if (ni.getState() == NetworkInfo.State.CONNECTED) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 获取当前网络类型
	 * 
	 * @return 0：没有网络 1：WIFI网络 2：WAP网络 3：NET网络
	 */
	public int getNetworkType() {
		int netType = 0;
		ConnectivityManager connectivityManager = (ConnectivityManager) CharmWordApplication.getInstance()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null) {
			return netType;
		}
		int nType = networkInfo.getType();
		if (nType == ConnectivityManager.TYPE_MOBILE) {
			String extraInfo = networkInfo.getExtraInfo();
			if (!StringUtils.isEmpty(extraInfo)) {
				if (extraInfo.toLowerCase().equals("cmnet")) {
					netType = NETTYPE_CMNET;
				} else {
					netType = NETTYPE_CMWAP;
				}
			}
		} else if (nType == ConnectivityManager.TYPE_WIFI) {
			netType = NETTYPE_WIFI;
		}
		return netType;
	}

	/**
	 * 获取手机唯一识别码
	 * 
	 * @return
	 */
	public String GetAndroidId(int bookid) {
		String android_id = Secure
				.getString(CharmWordApplication.getInstance().getContentResolver(), Secure.ANDROID_ID);

		if (android_id == null || android_id == "") {
			android_id = android.os.Build.VERSION.RELEASE + android.os.Build.MODEL + bookid;
		}
		if (StringUtils.isEmpty(android_id)) {
			android_id = "";
		}
		return android_id;
	}

	/**
	 * 获取手机的IMEI
	 * 
	 * @return
	 */
	public String getAndroidIMEI() {
		TelephonyManager telephonyManager = (TelephonyManager) CharmWordApplication.getInstance().getSystemService(
				Context.TELEPHONY_SERVICE);
		String imei = telephonyManager.getDeviceId();
		if (!StringUtils.isEmpty(imei)) {
			return imei;
		}
		return "";
	}

	/**
	 * 获取手机型号
	 * 
	 * @return
	 */
	public String GetMobileVersion() {
		return android.os.Build.MODEL;
	}

	/**
	 * 获取系统的版本号
	 * 
	 * @return
	 */
	public String GetAndroidVersion() {
		return android.os.Build.VERSION.RELEASE;
	}

	/**
	 * 获取App安装包信息
	 * 
	 * @return
	 */
	public PackageInfo getPackageInfo() {
		PackageInfo info = null;
		try {
			info = CharmWordApplication.getInstance().getPackageManager()
					.getPackageInfo(CharmWordApplication.getInstance().getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace(System.err);
		}
		if (info == null) {
			info = new PackageInfo();
		}
		return info;
	}

}
